package com.bizihang.common.security.core.code;


import com.bizihang.common.security.core.properties.SecurityProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 验证码生成器
 *
 * @author 毕子航 951755883@qq.com
 * @date 2018/08/18
 */
public abstract class AbstractValidateCodeGenerator implements ValidateCodeGenerator {
	/**
	 * 设置安全属性
	 */
	private SecurityProperties securityProperties;

	@Autowired
	public void setSecurityProperties(SecurityProperties securityProperties) {
		this.securityProperties = securityProperties;
	}

	public SecurityProperties getSecurityProperties() {
		return securityProperties;
	}

	/**
	 * 生成验证码的默认方法
	 *
	 * @return
	 */
	protected String generatorCode(int length) {
		int defaultLength = getSecurityProperties().getCode().getSms().getLength();
		if (length > 0) {
			defaultLength = length;
		}
		String code = RandomStringUtils.randomNumeric(defaultLength);
		return code;
	}
}
